package com.play.ballen.home.vm

import androidx.lifecycle.MutableLiveData
import com.play.ballen.R
import com.play.ballen.data.PImGroup
import com.play.ballen.home.entity.ArticleInfo
import com.play.ballen.home.entity.BottleCount
import com.play.ballen.home.entity.BottleData
import com.play.ballen.home.entity.BottleDetail
import com.play.ballen.home.entity.BottleGetGaoji
import com.play.ballen.home.entity.BottleGetJichu
import com.play.ballen.home.entity.BottleList
import com.play.ballen.home.entity.BottleListData
import com.play.ballen.home.entity.GasInfo
import com.play.ballen.home.entity.HomeIndexInfo
import com.play.ballen.home.entity.PlayCountInfo
import com.play.ballen.home.entity.Version
import com.play.ballen.network.ApiRepository
import com.play.ballen.network.BallenRxHelper
import com.play.ballen.utils.UserInfoManager
import com.yuven.appframework.busevent.SingleLiveEvent
import com.yuven.appframework.network.RxSchedulerHelper
import com.yuven.appframework.util.addToDisposable
import com.yuven.baselib.component.viewmodel.BaseViewModel
import com.yuven.baselib.utils.Logger
import com.yuven.baselib.utils.ResourceUtil
import okhttp3.MultipartBody

class PLPFragmentVM : BaseViewModel() {

    private val model = ApiRepository()
    val version = MutableLiveData<Version>()
    val bottleData = MutableLiveData<BottleData>()

    val bottleDetail = MutableLiveData<BottleDetail>()//捞到的瓶子详情
    val actionAddSuccess = SingleLiveEvent<Void>()
    val actionAddNameSuccess = SingleLiveEvent<Void>()
    val actionAddPicSuccess = SingleLiveEvent<Void>()
    val bottleListData = MutableLiveData<List<BottleList>>()
    val canGetNum = MutableLiveData<BottleCount>()
    val picToken = MutableLiveData<String>()

    val bottleList = MutableLiveData<List<BottleListData>?>()

    val pImGroup= MutableLiveData<PImGroup?>()

    val followSuccess = SingleLiveEvent<Void>()
    val unfollowSuccess = SingleLiveEvent<Void>()
    val deleteSuccess = SingleLiveEvent<Void>()
    val reportSuccess = SingleLiveEvent<Void>()
    val actionAddCommentSuccess = SingleLiveEvent<Void>()
    var page = 1
    private val pageSize = 20

    fun getuploadToken() {
        model.getuploadToken()
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .doFinally {
            }
            .subscribe({
                Logger.i("success")
                picToken.value = it.qnToken
            }, { throwable ->
            }).addToDisposable(compositeDisposable)
    }


    fun getBottle(id:String) {

        model.getBottle(id)
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .doFinally {

            }
            .subscribe({
                Logger.i("success")
                bottleDetail.value = it
            }, { throwable ->
                toastLiveData.value = throwable.message
                Logger.i("HomeFragmentVM.gasSubmit error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }

    fun seeBottle(id:String) {
        model.seeBottle(id)
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .doFinally {
            }
            .subscribe({
                Logger.i("success")
                bottleDetail.value = it
            }, { throwable ->
                toastLiveData.value = throwable.message
                Logger.i("HomeFragmentVM.gasSubmit error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }


    fun getBottlesalvage(map: MutableMap<String, Any>) {
        model.getBottlesalvage(map)
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .doFinally {
            }
            .subscribe({
                Logger.i("success")
                bottleDetail.value = it
            }, { throwable ->
                toastLiveData.value = throwable.message
                Logger.i("HomeFragmentVM.gasSubmit error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }

    fun getCanGetNum() {
        model.getCanGetNum()
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .doFinally {
            }
            .subscribe({
                Logger.i("success")
                canGetNum.value = it
            }, { throwable ->
                toastLiveData.value = throwable.message
                Logger.i("HomeFragmentVM.gasSubmit error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }




    //扔瓶子
    fun putBottle(
        map: MutableMap<String, Any>
    ) {
        showDialog.setValue(true, ResourceUtil.getString(R.string.loading), false)
        model.putBottle(map)
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .doFinally {
                showDialog.setValue(false)
                toastLiveData.value = "成功扔了一个瓶子"
                actionAddSuccess.call()
            }
            .subscribe({
                toastLiveData.value = "成功扔了一个瓶子"
                actionAddSuccess.call()
            }, { throwable ->
                toastLiveData.value = throwable.message
                Logger.i("HomeFragmentVM.gasSubmit error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }

    //获取水里的瓶子
    fun getbottleWater(
        map: MutableMap<String, Any>
    ) {
        model.getBottleWater(map)
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .doFinally {
            }
            .subscribe({
                bottleListData.value = it
            }, { throwable ->
                toastLiveData.value = throwable.message
                Logger.i("PLP.gasSubmit error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }


    fun addAvatar(avatar:String,width:String,height:String) {
        val map = mutableMapOf<String, Any>()
        map["avatar"] = avatar
        map["width"] = width.toInt()
        map["height"] = height.toInt()
        showDialog.setValue(true, ResourceUtil.getString(R.string.loading))
        model.addAvatar(map)
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .doFinally {
                showDialog.setValue(false)
            }
            .subscribe({
                toastLiveData.value = "上传成功"
                actionAddPicSuccess.call()
            }, { throwable ->
                toastLiveData.value = throwable.message
                Logger.i("PLP.gasSubmit error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }
    fun addName(nickname: String) {
        showDialog.setValue(true, ResourceUtil.getString(R.string.loading))
        val map = mutableMapOf<String, Any>()
        map["nickname"] = nickname
        model.addName(map)
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .doFinally {
                showDialog.setValue(false)
            }
            .subscribe({
                actionAddNameSuccess.call()
                Logger.i("success")
            }, { throwable ->
                toastLiveData.value = throwable.message
            }).addToDisposable(compositeDisposable)
    }


    fun getBottleList() {
        model.getBottleList(page,pageSize)
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .subscribe({ data ->
                bottleList.value = data
            }, { throwable ->
                toastLiveData.value = throwable.message
                Logger.i("ArticleVM.getArticleList error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }


    fun addComment(map: MutableMap<String, Any>) {
        showDialog.setValue(true, ResourceUtil.getString(R.string.loading))
        model.addComment(map)
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .doFinally {
                showDialog.setValue(false)
                actionAddCommentSuccess.call()
            }
            .subscribe({
                actionAddCommentSuccess.call()
                Logger.i("success")
            }, { throwable ->
                toastLiveData.value = throwable.message
            }).addToDisposable(compositeDisposable)
    }
    fun getGroupIm() {
        val users = mutableListOf<String>();
        users.add((bottleDetail.value?.user_id?:0).toString())
        UserInfoManager.getUserInfo()?.id?.let {
            users.add(it.toString())
        }
        model.getPrivateGroup(bottleDetail.value?.id?:0,users)
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .doFinally {
            }
            .subscribe({
                Logger.i("success")
                pImGroup.value = it
                //{"code":20000,"data":{"room_id":"231288249319427"},"msg":"ok"}
            }, { throwable ->
                toastLiveData.value = throwable.message
                Logger.i("HomeFragmentVM.gasSubmit error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }

    fun likeComment(id: String) {
        model.likeComment(id)
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .doFinally {
            }
            .subscribe({
                Logger.i("success")
            }, { throwable ->
                toastLiveData.value = throwable.message
                Logger.i("HomeFragmentVM.gasSubmit error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }
    fun unLikeComment(id: String) {
        model.unlikeComment(id)
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .doFinally {

            }
            .subscribe({
                Logger.i("success")
            }, { throwable ->
                toastLiveData.value = throwable.message
                Logger.i("HomeFragmentVM.gasSubmit error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }


    fun read(id: String) {
        model.read(id)
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .doFinally {
            }
            .subscribe({
                Logger.i("success")
            }, { throwable ->
                toastLiveData.value = throwable.message
                Logger.i("HomeFragmentVM.gasSubmit error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }



    fun follow(id: String) {
        model.follow(id)
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .doFinally {
                followSuccess.call()
            }
            .subscribe({
                Logger.i("success")
            }, { throwable ->
                toastLiveData.value = throwable.message
                Logger.i("HomeFragmentVM.gasSubmit error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }

    fun unfollow(id: String) {
        model.unfollow(id)
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .doFinally {
                followSuccess.call()
            }
            .subscribe({
                Logger.i("success")
            }, { throwable ->
                toastLiveData.value = throwable.message
                Logger.i("HomeFragmentVM.gasSubmit error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }


    fun reportBottle(map: MutableMap<String, Any>) {
        showDialog.setValue(true, ResourceUtil.getString(R.string.loading))
        model.reportBottle(map)
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .doFinally {
                toastLiveData.value = "举报成功"
                reportSuccess.call()
            }
            .subscribe({
                Logger.i("success")
            }, { throwable ->
                toastLiveData.value = throwable.message
                Logger.i("HomeFragmentVM.gasSubmit error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }


    fun followBottle(id: String) {
        model.followBottle(id)
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .doFinally {
                followSuccess.call()
            }
            .subscribe({
                Logger.i("success")
            }, { throwable ->
                toastLiveData.value = throwable.message
                Logger.i("HomeFragmentVM.gasSubmit error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }



    fun unfollowBottle(id: String) {
        model.unfollowBottle(id)
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .doFinally {
                followSuccess.call()
            }
            .subscribe({
                Logger.i("success")
            }, { throwable ->
                toastLiveData.value = throwable.message
                Logger.i("HomeFragmentVM.gasSubmit error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }

    fun deleteComment(id: String) {
        model.deleteComment(id)
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .doFinally {
                deleteSuccess.call()
                toastLiveData.value = "删除成功"
            }
            .subscribe({
                Logger.i("success")
            }, { throwable ->
                toastLiveData.value = throwable.message
                Logger.i("HomeFragmentVM.gasSubmit error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }




}